/**
 * 
 */
package org.swing.utility.jdbc.handler;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author lqnhu
 *
 */
public class ColumnListHandler extends AbstractListHandler<Object> {
	/**
	 * The column number to retrieve.
	 */
	private final int columnIndex;
	/**
	 * The column name to retrieve. Either columnName or columnIndex will be
	 * used but never both.
	 */
	private final String columnName;

	/**
	 * Creates a new instance of ColumnListHandler. The first column of each row
	 * will be returned from <code>handle()</code>.
	 */
	public ColumnListHandler() {
		this(1, null);
	}

	/**
	 * Creates a new instance of ColumnListHandler.
	 *
	 * @param columnIndex
	 *            The index of the column to retrieve from the
	 *            <code>ResultSet</code>.
	 */
	public ColumnListHandler(int columnIndex) {
		this(columnIndex, null);
	}

	/**
	 * Creates a new instance of ColumnListHandler.
	 *
	 * @param columnName
	 *            The name of the column to retrieve from the
	 *            <code>ResultSet</code>.
	 */
	public ColumnListHandler(String columnName) {
		this(1, columnName);
	}

	/**
	 * Private Helper
	 * 
	 * @param columnIndex
	 *            The index of the column to retrieve from the
	 *            <code>ResultSet</code>.
	 * @param columnName
	 *            The name of the column to retrieve from the
	 *            <code>ResultSet</code>.
	 */
	private ColumnListHandler(int columnIndex, String columnName) {
		super();
		this.columnIndex = columnIndex;
		this.columnName = columnName;
	}

	/**
	 * Returns one <code>ResultSet</code> column value as <code>Object</code>.
	 * 
	 * @param rs
	 *            <code>ResultSet</code> to process.
	 * @return <code>Object</code>, never <code>null</code>.
	 *
	 * @throws SQLException
	 *             if a database access error occurs
	 *
	 * @see org.apache.commons.dbutils.handlers.AbstractListHandler#handle(ResultSet)
	 */
	protected Object handleRow(ResultSet rs) throws SQLException {
		if (this.columnName == null) {
			return rs.getObject(this.columnIndex);
		} else {
			return rs.getObject(this.columnName);
		}
	}
}
